package com.kitty.game.shop;


import com.kitty.game.haoxinzhi.model.HaoXinZhiReward;
import com.kitty.game.mijing.model.MiJingReward;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.config.Medicine;
import com.kitty.game.config.NPC;
import com.kitty.game.config.OnlineMall;
import com.kitty.game.config.PetShop;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.bag.message.RespIconCartoon;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespNotifyMisc;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.map.service.MapService;
import com.kitty.game.pet.PetDataPool;
import com.kitty.game.pet.PetType;
import com.kitty.game.pet.bean.PetObject;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.AsktaoUtil;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.base.service.BagService;
import com.kitty.game.role.service.RoleService;
import org.apache.mina.core.session.IoSession;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * 购买物品
 */
@Controller
public class ShopController {
    Logger logger = LoggerFactory.getLogger(ShopController.class);

    /**
     * 购买野生宠物
     *
     * @param session
     * @param reqBuyPet
     */
    @RequestMapping
    public void buyPet(IoSession session, ReqBuyPet reqBuyPet) {
        Role role = SessionUtils.getRoleBySession(session);
        if (SpringUtils.getPetService().isEquippedFull(role)) {
            MessagePusher.pushMessage(session, new RespMsg("对不起，你的宠物栏满了，请整理后再来吧。"));
        } else {
            PetService petService = SpringUtils.getBean(PetService.class);
            RoleService roleService = SpringUtils.getBean(RoleService.class);
            int xiaohaohaoxinzhi;
            if (reqBuyPet.getType() == 7) {
                MiJingReward miJingReward = DataCache.mijing_GOODS.get(reqBuyPet.getName());
                xiaohaohaoxinzhi = reqBuyPet.getNum() * miJingReward.getPrice();
                if (role.getMijingjifen() < xiaohaohaoxinzhi) {
                    MessagePusher.pushMessage(session, new RespMsg("奇门积分不足，无法购买"));
                } else {
                    SpringUtils.getRoleService().submijingjifen(role, xiaohaohaoxinzhi, true);
                    MessagePusher.pushMessage(session, new RespMsg("消耗#R" + xiaohaohaoxinzhi + "#n奇门积分"));
                    SpringUtils.getEquipService().addMall(reqBuyPet.getName(), true, role, reqBuyPet.getNum());
                }
            } else if (reqBuyPet.getType() == 2) {
                HaoXinZhiReward haoXinZhiReward = DataCache.HAOXINZHI_GOODS.get(reqBuyPet.getName());
                xiaohaohaoxinzhi = reqBuyPet.getNum() * haoXinZhiReward.getPrice();
                if (role.getNice() < xiaohaohaoxinzhi) {
                    MessagePusher.pushMessage(session, new RespMsg("好心值不足，无法购买"));
                } else {
                    SpringUtils.getRoleService().submihaoxianzhi(role, xiaohaohaoxinzhi, true);
                    MessagePusher.pushMessage(session, new RespMsg("消耗#R" + xiaohaohaoxinzhi + "#n好心值"));
                    SpringUtils.getEquipService().addMall(reqBuyPet.getName(), true, role, reqBuyPet.getNum());
                }
            } else {
                PetObject petObject = PetDataPool.getPetObject(reqBuyPet.getName());
                if (petObject != null && petObject.getPetType() == 2) {
                    PetShop petShop = ShopDataPool.getPetShopByName(reqBuyPet.getName());
                    int DBMoney = role.isUseVoucher() ? role.getVoucher() : role.getMoney();
                    if (DBMoney < petShop.getMoney()) {
                        MessagePusher.pushMessage(session, new RespMsg("你的金钱不足！请到积分商城购买"));
                    } else {
                        if (petObject.getLevel_req() - role.getLevel() >= 15) {
                            MessagePusher.pushMessage(session, new RespMsg("你的等级没有达到该宠物携带等级。"));
                            return;
                        }

                        if (role.isUseVoucher()) {
                            role.setVoucher(DBMoney - petShop.getMoney());
                        } else {
                            role.setMoney(DBMoney - petShop.getMoney());
                        }

                        Pet pet = petService.addWildPet(petObject, role);
                        RespIconCartoon respIconCartoon = new RespIconCartoon();
                        respIconCartoon.setType((short)2);
                        respIconCartoon.setName(reqBuyPet.getName());
                        respIconCartoon.setParam(petObject.getIcon() + "");
                        MessagePusher.pushMessage(session, respIconCartoon);
                        petService.loadPet(role, pet);
                        String msg = AsktaoUtil.getMoneyFormat(petShop.getMoney());
                        MessagePusher.pushMessage(session, new RespNotifyMiscEx("你花费了" + msg + "#n文" + (role.isUseVoucher() ? "代金券" : "金钱") + "购买了一只#Y" + pet.getName() + "（野生）#n。"));
                        roleService.updateRoleMoney(role);
                        role.save();
                    }

                }
            }
        }
    }

    /**
     * 购买道具 药品
     *
     * @param session
     * @param reqBuyItem
     */
    @RequestMapping
    public void buyMedicine(IoSession session, ReqBuyItem reqBuyItem) {
        MapService mapService = SpringUtils.getBean(MapService.class);
        RoleService roleService = SpringUtils.getBean(RoleService.class);
        BagService bagService = SpringUtils.getBean(BagService.class);
        Role role = SessionUtils.getRoleBySession(session);
        NPC npc = mapService.getNpc(reqBuyItem.getShipper());
        short pos = reqBuyItem.getPos();
        short num = reqBuyItem.getAmount();
        if (num <= 0 || num > 100) {
            return;
        }
        if (npc.getId() == 14443 ||npc.getId() == 14881 || npc.getId() == 14806 || npc.getId() == 14618) { //杂货店
            SpringUtils.getBean(ShopService.class).buyGrocery(role, pos, num);
            return;
        }
        if (pos >= npc.getList().size()) {
            return;
        }
        List<String> list = npc.getList();
        boolean isDaijinjuan = role.isUseVoucher();//真 使用代金券 假 使用金钱

        Medicine medicine = ShopDataPool.getMedicineBy(list.get(pos - 1));
        if (medicine == null) {
            return;
        }

        int needMoney = medicine.getPrice() * num;
        if (isDaijinjuan) {
            int DBDaijinquan = role.getVoucher();
            if (DBDaijinquan < needMoney) {
                RespMsg respMsg = new RespMsg();
                respMsg.setMsg("你的代金券不足。");
                MessagePusher.pushMessage(session, respMsg);
                return;
            } else {
                role.setVoucher(DBDaijinquan - needMoney);
            }
        } else {
            int DBMoney = role.getMoney();
            if (DBMoney < needMoney) {
                RespMsg respMsg = new RespMsg();
                respMsg.setMsg("你的金钱不足！请到积分商城购买");
                MessagePusher.pushMessage(session, respMsg);
                return;
            } else {
                roleService.subtractMoney(role,  needMoney);
            }
        }
        String msg = AsktaoUtil.getMoneyFormat(needMoney);
        RespNotifyMisc respNotifyMiscEx = new RespNotifyMisc();
        respNotifyMiscEx.setMsg("你购买了#R" + num + "#n" + medicine.getUnit() + "#R" + medicine.getKey_name() + "#n，花费" + msg + "#n文" + (isDaijinjuan ? "代金券。" : "钱。"));
        respNotifyMiscEx.setTime(new Long(System.currentTimeMillis() / 1000).intValue());
        MessagePusher.pushMessage(session, respNotifyMiscEx);
        roleService.updateRoleMoney(role);
        OnlineMall onlineMall = new OnlineMall();
        onlineMall.setName(medicine.getKey_name());
        if (medicine.getKey_name().contains("池")) {
            onlineMall.setType(310);
        } else {
            onlineMall.setType(524598);
            onlineMall.setItemtype((short) 2);
        }

        bagService.addRoleEquip(role, !isDaijinjuan, num, onlineMall);
        MessagePusher.pushMessage(session, new RespIconCartoon(onlineMall.getName()));
    }


    /**
     * 购买如意刷道令
     *
     * @param session
     * @param reqBuyRuyi
     */
    @RequestMapping
    public void buyRuyishuadao(IoSession session, ReqBuyRuyi reqBuyRuyi) {
        if (reqBuyRuyi.getNum() <= 0) {
            return;
        }
        Role role = SessionUtils.getRoleBySession(session);
        RoleService roleService = SpringUtils.getBean(RoleService.class);
        int DBMoney = role.getMoney();
        if (DBMoney < 2000000 * reqBuyRuyi.getNum()) {
            return;
        }
        roleService.subtractMoney(role,  2000000 * reqBuyRuyi.getNum());
        NutMap nutMap = role.getPropsStatus();
        int ruyi = nutMap.getInt("ruyi", 0);
        ruyi += 200 * reqBuyRuyi.getNum();
        nutMap.setv("ruyi", ruyi);
        role.setPropsStatus(nutMap);
        role.save();

        ArrayList<FiedValue> list = new ArrayList<>();
        FiedValue fiedValue = new FiedValue();
        fiedValue.setType((short) 918);
        fiedValue.setVT((byte) 3);
        fiedValue.setValue((short) nutMap.getInt("ruyi"));
        list.add(fiedValue);
        RespUpdate respUpdate = new RespUpdate();
        respUpdate.setRoleId(role.getRoleId());
        respUpdate.setList(list);
        MessagePusher.pushMessage(session, respUpdate);

        MessagePusher.pushMessage(session, new RespNotifyMiscEx("你花费了" + AsktaoUtil.getMoneyFormat(2000000 * reqBuyRuyi.getNum())
                + "文钱购买#R" + 200 * reqBuyRuyi.getNum() + "#n点如意刷道令点数。"));
    }
}
